﻿Imports System
Imports System.Data
Imports System.Data.OleDb
Imports QuanLyNhaSachDTO
Public Class TheLoaiSachDAO
#Region "Duyệt Danh Sách"
    Public Shared Function DaTatableToListof(ByVal Table As DataTable) As List(Of TheLoaiSach)
        Dim arr As New List(Of TheLoaiSach)
        For Each row As DataRow In Table.Rows
            Dim theloai As New TheLoaiSach
            theloai.TheLoaiSach = row("TheLoaiSach")
            arr.Add(theloai)
        Next
        Return arr
    End Function

    Public Function LayDanhSach() As List(Of TheLoaiSach)
        Dim arr As New List(Of TheLoaiSach)
        Dim da As New DataTable
        Dim cn As OleDbConnection = Dataprovider.connetion()
        Dim sql As String = "select * from TheLoaiSach"
        Dim cmd As New OleDbCommand(sql, cn)
        Dim adr As New OleDbDataAdapter
        adr.SelectCommand = cmd
        adr.Fill(da)
        arr = DaTatableToListof(da)
        cn.Close()
        Return arr
    End Function

#End Region

#Region "Update Danh Sách"

    Public Function ThemTheLoaiSach(ByVal theloai As String) As Exception
        Dim ex As New Exception
        Dim cn As OleDbConnection = Dataprovider.connetion()
        Dim sql As String = "insert into TheLoaiSach(TheLoaiSach) values(?)"
        Dim cmd As New OleDbCommand(sql, cn)
        cmd.Parameters.Add("@TheLoaiSach", OleDbType.WChar)
        cmd.Parameters("@TheLoaiSach").Value = theloai
        If cmd.ExecuteNonQuery() <> 0 Then
            ex = New Exception("Thêm Thể Loại sách Thành Công (TheLoai: " & theloai & ")")
        Else
            ex = New Exception("Thêm Thể Loại sách Thất Bại (TheLoai: " & theloai & ")")
        End If
        cn.Close()
        Return ex
    End Function

    Public Function ThemTheLoaiSach(ByVal theloai As TheLoaiSach) As Exception
        Return ThemTheLoaiSach(theloai.TheLoaiSach)
    End Function

    Public Function XoaTheLoaiSach(ByVal theloai As String) As Exception
        Dim ex As New Exception
        Dim cn As OleDbConnection = Dataprovider.connetion()
        Dim sql As String = "delete from TheLoaiSach where TheLoaiSach = ?"
        Dim cmd As New OleDbCommand(sql, cn)
        cmd.Parameters.Add("@TheLoaiSach", OleDbType.WChar)
        cmd.Parameters("@TheLoaiSach").Value = theloai
        If cmd.ExecuteNonQuery() <> 0 Then
            ex = New Exception("Thêm Thể Loại sách Thành Công (TheLoai: " & theloai & ")")
        Else
            ex = New Exception("Thêm Thể Loại sách Thất Bại (TheLoai: " & theloai & ")")
        End If
        cn.Close()
        Return ex
    End Function

    Public Function XoaTheLoaiSach(ByVal theloai As TheLoaiSach) As Exception
        Return XoaTheLoaiSach(theloai.TheLoaiSach)
    End Function
#End Region

End Class
